package com.example.jms;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;

import com.alibaba.fastjson.JSONObject;
/**
 * 
 * @author qxw
 * 2017年10月24日
 */
public class State {

	
	
	
	public static void main(String[] args) throws IOException, MalformedObjectNameException {
//		String url = "service:jmx:rmi:///jndi/rmi://localhost:11099/jmxrmi";
//		JMXServiceURL urls = new JMXServiceURL(url);
//		JMXConnector connector = JMXConnectorFactory.connect(urls,null);
//		connector.connect();
//		MBeanServerConnection conn = connector.getMBeanServerConnection();
//		//这里brokerName的b要小些，大写会报错
//	    ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");
//		BrokerViewMBean mBean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class, true);
//		//获取点对点的队列  mBean.getQueues()		mBean.getTopics() 获取订阅模式的队列
//		for(ObjectName na : mBean.getQueues()){
//			QueueViewMBean queueBean = (QueueViewMBean) 
//			MBeanServerInvocationHandler.newProxyInstance(conn, na, QueueViewMBean.class, true);
//			System.out.println("******************************");
//			System.out.println("队列的名称："+queueBean.getName());
//			System.out.println("队列中剩余的消息数："+queueBean.getQueueSize());
//			System.out.println("消费者数："+queueBean.getConsumerCount());
//			System.out.println("出队列的数量："+queueBean.getDequeueCount());
		
		System.out.println(getQueueList().toString());
//		}
	}
	
	
	
	/**
	 * 获取点对点的队列虽有信息(队列的名称,队列中剩余的消息数,消费者数,出队列的数量)
	 * @param name 队列的名称
	 * @param queueSize  队列中剩余的消息数
	 * @param consumerCount 消费者数
	 * @param dequeueCount  出队列的数量
	 * @return
	 * 2017年10月24日
	 */
	public static List<JSONObject> getQueueList() throws IOException, MalformedObjectNameException{
		List<JSONObject> list=new ArrayList<JSONObject>();
		JSONObject json=new JSONObject();
		String url = "service:jmx:rmi:///jndi/rmi://localhost:11099/jmxrmi";
		JMXServiceURL urls = new JMXServiceURL(url);
		JMXConnector connector = JMXConnectorFactory.connect(urls,null);
		connector.connect();
		MBeanServerConnection conn = connector.getMBeanServerConnection();
		//这里brokerName的b要小些，大写会报错
	    ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");
		BrokerViewMBean mBean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class, true);
		//获取点对点的队列  		mBean.getTopics() 获取订阅模式的队列
		for(ObjectName na : mBean.getQueues()){
			QueueViewMBean queueBean = (QueueViewMBean) 
			MBeanServerInvocationHandler.newProxyInstance(conn, na, QueueViewMBean.class, true);
			json.put("name", queueBean.getName());
			json.put("queueSize", queueBean.getQueueSize());
			json.put("consumerCount", queueBean.getConsumerCount());
			json.put("dequeueCount", queueBean.getDequeueCount());
			list.add(json);
		}
		return list;
	}
	
	
	/**
	 * 获取订阅模式的队列
	 * @param name 队列的名称
	 * @param queueSize  队列中剩余的消息数
	 * @param consumerCount 消费者数
	 * @param dequeueCount  出队列的数量
	 * @return
	 * 2017年10月24日
	 */
	public static List<JSONObject> getTopicsList() throws IOException, MalformedObjectNameException{
		List<JSONObject> list=new ArrayList<JSONObject>();
		JSONObject json=new JSONObject();
		String url = "service:jmx:rmi:///jndi/rmi://localhost:11099/jmxrmi";
		JMXServiceURL urls = new JMXServiceURL(url);
		JMXConnector connector = JMXConnectorFactory.connect(urls,null);
		connector.connect();
		MBeanServerConnection conn = connector.getMBeanServerConnection();
		//这里brokerName的b要小些，大写会报错
	    ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");
		BrokerViewMBean mBean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class, true);
		for(ObjectName na : mBean.getTopics()){
			QueueViewMBean queueBean = (QueueViewMBean) 
			MBeanServerInvocationHandler.newProxyInstance(conn, na, QueueViewMBean.class, true);
			json.put("name", queueBean.getName());
			json.put("queueSize", queueBean.getQueueSize());
			json.put("consumerCount", queueBean.getConsumerCount());
			json.put("dequeueCount", queueBean.getDequeueCount());
			list.add(json);
		}
		return list;
	}
	
}
